package com.csc.banking.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.csc.banking.dao.SystemUserDAO;
import com.csc.banking.domain.SystemUser;
import com.csc.banking.util.CustomEntityManager;

@Repository
@Transactional
public class SystemUserDAOImpl extends CustomEntityManager implements SystemUserDAO {
	@Override
	public boolean login(String userName, String password){
		// Obtains entity manager object
				EntityManager entityManager = getEntityManager();

				// Obtains transaction from entity manager
				EntityTransaction entr = entityManager.getTransaction();

				// -----------Begin transaction-----------
				
				boolean check = false;
				try {
					entr.begin();
					// Get a list of accounts from DB

					TypedQuery<SystemUser> query = entityManager
							.createQuery(
							"SELECT us FROM " + SystemUser.class.getName()
									+ " us Where us.userName= :username and us.password= :password", SystemUser.class);
					query.setParameter("username", userName);
					query.setParameter("password", password);
					check = query.getResultList().size()>0 ;
					entr.commit();
				} catch (Exception e) {
					entityManager.close();
				}
				// -----------End transaction-----------

				return check;
	}
	
}
